package org.qloudgen.txn.dilithium.cluster.common;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.qloudgen.txn.dilithium.cluster.common.LogPrintFile;

/*
 * Execute linux shell command.
 */

public final class ExecShellCommand {

	static Log log = LogFactory.getLog(ExecShellCommand.class);
	
    /*
     * execShellCommandWithReturn
     * Execute command_org and write the result on log-file
     * and return the result.
     */

    public static int execShellCommandWithReturn(final String[] command_org) {
        List<String> command = new ArrayList<String>();
        for (int i = 0; i < command_org.length; i++) {
            command.add(command_org[i]);
        }

        ProcessBuilder process_builder = new ProcessBuilder(command);
        process_builder.redirectErrorStream(true);
        InputStreamReader input_stream_reader = null;

        if (log.isDebugEnabled()){
        	for (int i = 0 ; i < command_org.length; i++){
        		log.debug(command_org[i]);
        	}
        }
        
        try {
            Process process = process_builder.start();
            input_stream_reader = new InputStreamReader(
                    process.getInputStream(), "UTF-8");
            while (true) {
                int c = input_stream_reader.read();
                if (c == -1) {
                    input_stream_reader.close();
                    break;
                }
            }
            process.waitFor();
            int ret = process.exitValue();
            return ret;
        } catch (Exception e) {
        	return -1;
        }
    }

    /*
     * execshellCommandWithPrintLogToWeb
     * Execute command_org and write the result on log-file and show the result.
     */

    public static int execShellCommandWithPrintLogToWeb(final String[] command_org,String log_file_name) {
    	
    	List<String> command = new ArrayList<String>();
        for (int i = 0; i < command_org.length; i++) {
            command.add(command_org[i]);
        }

        if (log.isDebugEnabled()){
        	for (int i = 0 ; i < command_org.length; i++){
        		log.debug(command_org[i]);
        	}
        }
        
        ProcessBuilder process_builder = new ProcessBuilder(command);
        process_builder.redirectErrorStream(true);
        InputStreamReader input_stream_reader = null;
        StringBuffer result = new StringBuffer();

        try {
            Process process = process_builder.start();
            input_stream_reader =
                    new InputStreamReader(process.getInputStream(), "UTF-8");
            while (true) {
                int c = input_stream_reader.read();
                if (c == -1) {
                    input_stream_reader.close();
                    break;
                }
                result.append(String.valueOf((char) c));
                
                if ( c == 10){ 
                	 if (log_file_name != null)            
                     	LogPrintFile.addLogInfo(log_file_name,result.toString());

                     result.setLength(0);
                }
            }
            process.waitFor();
            
            int ret = process.exitValue();
            if (ret != 0) {
            	return -1;
            }
        } catch (Exception e) {
           e.printStackTrace();
        }
        return 0;
    }
    

    public static int execShellCommandWithReturnLog(final String[] command_org,StringBuffer sb) {
    	
    	List<String> command = new ArrayList<String>();
        for (int i = 0; i < command_org.length; i++) {
            command.add(command_org[i]);
        }
        
        if (log.isDebugEnabled()){
        	for (int i = 0 ; i < command_org.length; i++){
        		log.debug(command_org[i]);
        	}
        }
        
        ProcessBuilder process_builder = new ProcessBuilder(command);
        process_builder.redirectErrorStream(true);
        InputStreamReader input_stream_reader = null;

        try {
            Process process = process_builder.start();
            input_stream_reader =
                    new InputStreamReader(process.getInputStream(), "UTF-8");
            while (true) {
                int c = input_stream_reader.read();
                if (c == -1) {
                    input_stream_reader.close();
                    break;
                }
                
                sb.append(String.valueOf((char) c));
                
            }
            process.waitFor();
            log.debug(sb.toString());
            
            int ret = process.exitValue();
            if (ret != 0) {
            	return -1;
            }
        } catch (Exception e) {
           e.printStackTrace();
        }
        return 0;
    }
}
